package org.jeecg.modules.platform.config.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.platform.config.service.ICyPlatformRouteConfigService;
import org.jeecg.modules.platform.entity.CyPlatformRouteConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Description: 平台路由配置表
 * @Author: jeecg-boot
 * @Date: 2024-10-30
 * @Version: V1.0
 */
@Api(tags = "平台路由配置表")
@RestController
@RequestMapping("/platform/config/cyPlatformRouteConfig")
@Slf4j
public class CyPlatformRouteConfigController extends JeecgController<CyPlatformRouteConfig, ICyPlatformRouteConfigService> {

    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.PUT})
    public Result<Boolean> save(@RequestBody CyPlatformRouteConfig routeConfig) {
        if (routeConfig.getType() == null) {
            return Result.error("未选择类型");
        }

        CyPlatformRouteConfig one = service.getOne(new LambdaQueryWrapper<>(new CyPlatformRouteConfig().setType(routeConfig.getType())));
        if (Objects.nonNull(one)) {
            routeConfig.setId(one.getId());
        }
        return Result.OK(service.saveOrUpdate(routeConfig));
    }

    @GetMapping(value = "get")
    public Result<CyPlatformRouteConfig> get(@RequestParam String type) {
        return Result.OK(service.getOne(new LambdaQueryWrapper<CyPlatformRouteConfig>().eq(CyPlatformRouteConfig::getType, type).last("limit 1")));
    }
}
